﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dao;
using Domain;
using System.IO;

namespace StampApp
{
    public partial class NewProduct : Form
    {
        public NewProduct()
        {
            InitializeComponent();
            GridLoad();
        }

        private void button7_Click(object sender, EventArgs e)
        {

            if (txt_Title.Text.Length == 0
                || txt_Sku.Text.Length == 0
                || txt_StartPrice.Text.Length == 0
                || txt_Quantity.Text.Length == 0
                || txt_Image.Text.Length == 0
                || rTxt_Desc.Text.Length == 0


                )
            {

                MessageBox.Show("please enter value!");

                return;
            }

            try
            {

                InvProductDao productDao = new InvProductDao();

                InvProduct cp = new InvProduct();
                cp.productId = Guid.NewGuid();
                cp.buyItNow = Convert.ToDecimal(txt_BuyItNow.Text);
                cp.createDate = DateTime.Now;
                cp.description = rTxt_Desc.Text;
                cp.image = txt_Image.Text;
                cp.quantity = Convert.ToInt32(txt_Quantity.Text);
                cp.sku = txt_Sku.Text;
                cp.startPrice = Convert.ToDecimal(txt_StartPrice.Text);
                cp.status = 0;
                cp.title = txt_Title.Text;
                cp.updateDate = DateTime.Now;

                productDao.Save(cp);

                GridLoad();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void GridLoad()
        {


            InvProductDao productDao = new InvProductDao();

            this.dataGridView1.DataSource = new SortableBindingList<InvProduct>(productDao.LoadAll());

            this.dataGridView1.Sort(this.dataGridView1.Columns["updatedate"], ListSortDirection.Descending);

            if (!this.dataGridView1.Columns.Contains("index"))            {

                this.dataGridView1.Columns.Add("index", "id");
            }
            this.dataGridView1.Columns["index"].DisplayIndex = 0;
            this.dataGridView1.Columns["sku"].DisplayIndex = 1;

        }

        private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        string _sDirectory;
        private void but_import_Click(object sender, EventArgs e)
        {
            _sDirectory = System.IO.Directory.GetCurrentDirectory();
            
            this.openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

            MessageBox.Show(this.openFileDialog1.FileName);
            StreamReader sr = new StreamReader(this.openFileDialog1.FileName, System.Text.Encoding.Default);

            System.IO.Directory.SetCurrentDirectory(_sDirectory);


            string strLine = sr.ReadLine();
            while (strLine != null)
            {
                string[] ss = strLine.Split(',');

                InvProduct newProduct = new InvProduct();

                newProduct.sku = ss[0];
                newProduct.title = ss[1];
                newProduct.buyItNow = Convert.ToDecimal(ss[2]);
                newProduct.quantity = Convert.ToInt32(ss[3]);
                newProduct.productId = Guid.NewGuid();
                newProduct.startPrice = Convert.ToDecimal(0.99); ;
                newProduct.image = ss[4];
                newProduct.description = ss[5];
                newProduct.status = 0;
                newProduct.createDate = DateTime.Now;
                newProduct.updateDate = DateTime.Now;

                InvProductDao productDao = new InvProductDao();

                productDao.Save(newProduct);

                strLine = sr.ReadLine();
            }
            sr.Close();

            GridLoad();

        }

        private void but_Delete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow iRow in dataGridView1.SelectedRows)
            {

                Guid pid = new Guid(iRow.Cells["productid"].Value.ToString());

                InvProduct elib = new InvProduct();
                elib.productId = pid;

                InvProductDao productDao = new InvProductDao();

                productDao.Delete(elib);
            }

            GridLoad();
        }
    }
}
